package com.ctg.behavior.calc.impala.builder.properties;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.utils.CtCalcPropertyUtils;
import com.ctg.behavior.common.CtgMapData;
import com.ctg.behavior.common.calc.CtCalcPropertyType;
import com.ctg.behavior.common.consts.CtPropertyConsts;
import com.ctg.behavior.common.properties.CtUserPropertiesCalcBody;
import com.ctg.behavior.common.properties.CtUserPropertiesCalcResult;
import com.ctg.behavior.utils.CtDateUtils;

@Component
public class CtUesrPropertyBuilder {

	public CtUserPropertiesCalcResult doBuilder(CtUserPropertiesCalcResult calcResult, List<LinkedHashMap<String, Object>> queryResults, CtUserPropertiesCalcBody propertyCalcBody) {

		calcResult.setValue(new CtgMapData());
		
		calcResult.getValue().setAppId(propertyCalcBody.getAppId());
		calcResult.getValue().setBookmarkId(propertyCalcBody.getBookmarkId());
		calcResult.getValue().setRequestId(propertyCalcBody.getRequestId());
		calcResult.getValue().setDataUpdateTime(CtDateUtils.dateToString(new Date()));
		calcResult.getValue().setReportUpdateTime(CtDateUtils.dateToString(new Date()));
		
		if(0 == queryResults.size()) {
			return calcResult;
		}
		
		for(String field : propertyCalcBody.getProfiles()) {
			CtCalcPropertyType pType = CtCalcPropertyUtils.getValueType(field);
			if(!CtPropertyConsts.PROPERTY_USER.equals(pType.getType())){
				continue;
			}
			String pValue = "";
			if( null != queryResults.get(0).get(pType.getProperty())){
				pValue = queryResults.get(0).get(pType.getProperty()).toString();
			}
			calcResult.getValue().getMap().put(field, pValue);
		}

		return calcResult;
	}
}
